#include "StdThread.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#include <sys/shm.h>
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
int money=10000;
void *thread1(void *arg)
{
    printf("current money:%d\n",money);
    sleep(1);
    pthread_mutex_lock(&mutex);
    int mymoney=money;
    printf("zhang take the money\n");
    sleep(1);
    if(mymoney>=10000)
    {
        money=0;
        printf("zhang take 10000,   money%d\n",money);
    }
    else
    {
        printf("zhang take 0,   money%d\n",money);
    }
    pthread_mutex_unlock(&mutex);
    return NULL;
}

void *thread2(void *arg)
{
    printf("current money:%d\n",money);
    sleep(1);
    pthread_mutex_lock(&mutex);
    int mymoney=money;
    printf("li take the money\n");
    sleep(1);
    if(mymoney>=10000)
    {
        money=0;
        printf("li take 10000,   money%d\n",money);
    }
    else
    {
        printf("li take 0,   money%d\n",money);
    }
    pthread_mutex_unlock(&mutex);
    return NULL;
}
void main()
{

    thread *t1=InitThread(thread1,NULL);
    thread *t2=InitThread(thread2,NULL);

    JoinThread(t1);
    JoinThread(t2);
    pthread_mutex_destroy(&mutex);
}